/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 10, 2012  10:08:14 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.sales.receipted
 * File Name : ReceiptedInstallmentProcessing.java
 * TODO
 */
package apps.business.process.sales.receipted;

import java.util.List;
import jpa.entitys.promis.common.VwSyncRcvdInstallment;
import jpa.entitys.synchro.staging.FiArreceiptreOutb;
import jpa.entitys.synchro.staging.PsWbsMaster;
import jpa.entitys.synchro.staging.SdChargecode;
import org.apache.log4j.Logger;

import promis.utility.CommonService;
import sap.business.fi.outbound.FiArreceiptreOutbService;
import sap.business.ps.outbound.PsWbsMasterService;
import sap.business.sd.outbound.SdChargecodeService;
import apps.business.process.SyncProcessorCommons;
import apps.promis.out.common.PropertyUnitAction;
import apps.promis.out.common.VwSyncRcvdInstallmentAction;
import apps.promis.out.sales.ReceiptedChargeCodeAction;

/**
 * @author chatree
 *
 */
public class ReceiptedInstallmentProcessing extends SyncProcessorCommons {
		
	private static final Logger log = Logger.getLogger(ReceiptedInstallmentProcessing.class);
	
	
	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA";  //SPLCITY-SRI
			String ReceipteddocumentNo = "RR-1206-000079";
			String status = "A";
			String syncIsstaging = "Y";
	 * @param company
	 * @param project
	 * @param ReceipteddocumentNo
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	public void Processing(String company, String project, String ReceipteddocumentNo, String status, String syncIsstaging) throws Exception {
		System.out.println("Start of Receipt Processing --->");
		try{
			
			//Find Receipt
			List<VwSyncRcvdInstallment> lstt = new VwSyncRcvdInstallmentAction().findRcvdInstallmentByDocumentNo(company, project, ReceipteddocumentNo, status, syncIsstaging);
			System.out.println("Find Installment By DocumentNo [company="+ company + " || project="+ project +" || ReceipteddocumentNo="+ ReceipteddocumentNo +" || status="+ status +" || syncIsstaging="+ syncIsstaging +"] Size=" + CommonService.getSize(lstt));
			
			if(!CommonService.IsNull(lstt)){
				
				//Property Unit///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
				PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
				List<PsWbsMaster> unitLst = propertyUnitAction.CreatedToStaging(company, project, lstt.get(0).getBuildingPhaseCode(), lstt.get(0).getFloorZoneCode(), lstt.get(0).getUnitNumber());
				System.out.println("Find Property Unit:[Size=" + CommonService.getSize(unitLst) + "] --> ");
				
				if(!CommonService.IsNull(unitLst)){					
					new PsWbsMasterService().SendingToSAP(unitLst);
					System.out.println("Sending Property Unit:[Company=" + unitLst.get(0).getCompany() + "|| Project=" + unitLst.get(0).getProject() + "|| BuildingPhase=" + unitLst.get(0).getBuildingPhase() + "|| FloorZone=" + unitLst.get(0).getFloorZone() + "|| UnitNumber=" + unitLst.get(0).getUnitNumber() + " || Size=" + CommonService.getSize(unitLst) + " --> ");
				}
				
				//Create to CreateRcvdToStaging
				//List<FiArreceiptreOutb> RR_Installment_lst = new VwSyncRcvdInstallmentAction().CreateRcvdToStaging(company, project, ReceipteddocumentNo, status, syncIsstaging);
				List<FiArreceiptreOutb> RR_Installment_lst = new VwSyncRcvdInstallmentAction().CreateRcvdToStaging(lstt);
				System.out.println("Created Rcvd Installment To Staging  Size="+ CommonService.getSize(RR_Installment_lst) +" --->");
				
				if(!CommonService.IsNull(RR_Installment_lst)){
				
					//Create ChargeCode To Staging 
					ReceiptedChargeCodeAction receiptedChargeCodeAction = new ReceiptedChargeCodeAction();			
				    List<SdChargecode> chargecodeList = receiptedChargeCodeAction.CreateToStaging(RR_Installment_lst);
					
					//Sending ChargeCode to SAP (SD)
					SdChargecodeService SdChargecodeServ = new SdChargecodeService();
					SdChargecodeServ.SendingToSAP(chargecodeList);
					System.out.println("Sending Rcvd ChargeCode To Staging Size = "+ CommonService.getSize(chargecodeList) +" --->");
					
					//Send to RCVD SAP (FI)
					new FiArreceiptreOutbService().SendingToSAP(RR_Installment_lst);
					System.out.println("Sending Rcvd Installment To SAP Size = "+ CommonService.getSize(RR_Installment_lst) +" --->");					
				}
			}
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}finally{
			System.out.println("End of Receipt Processed <----");
		}
	}
	
	public void SendAllWithNonSending() throws Exception {
		try{
			List<Object[]> lst = new VwSyncRcvdInstallmentAction().SearchAllWithNonSending();
			log.info("SearchAllWithNonSending size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0){
				for(Object[] value : lst){
					
					String company = value[0].toString();
					String project = value[1].toString();
					String receipteddocumentNo = value[2].toString();
					String status = value[3].toString();
					String syncIsstaging = value[4].toString();
					
					log.info("company="+ company + " || project="+ project +" || receipteddocumentNo="+ receipteddocumentNo +" || status="+ status +" || syncIsstaging=" + syncIsstaging);
					System.out.println("company="+ company + " || project="+ project +" || receipteddocumentNo="+ receipteddocumentNo +" || status="+ status +" || syncIsstaging=" + syncIsstaging);
					
					Processing(company, project, receipteddocumentNo, status, syncIsstaging);
					
				}
			}			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	public void ProcessingNotSyncInStaging() throws Exception {
		try{
			
			List<Object[]> lst = new VwSyncRcvdInstallmentAction().SearchNotSyncInStaging();
			log.info("SearchNotSyncInStaging size=" + CommonService.getSize(lst));
			
			for(Object[] value : lst){
				String instanceNo = value[3].toString();
				List<FiArreceiptreOutb> newlist = new VwSyncRcvdInstallmentAction().findInstanceNo1(instanceNo);
				
				if(!CommonService.IsNull(newlist)){
				
					//Create ChargeCode To Staging 
				    List<SdChargecode> chargecodeList = new ReceiptedChargeCodeAction().CreateToStaging(newlist);
					
					//Sending ChargeCode to SAP (SD)
				    new SdChargecodeService().SendingToSAP(chargecodeList);
					System.out.println("Sending Rcvd ChargeCode To Staging Size = "+ CommonService.getSize(chargecodeList) +" --->");
					log.info("Sending Rcvd ChargeCode To Staging Size = "+ CommonService.getSize(chargecodeList) +" --->");					
					
					//Send to RCVD SAP (FI)
					new FiArreceiptreOutbService().SendingToSAP(newlist);
					System.out.println("Sending Rcvd Installment To SAP Size = "+ CommonService.getSize(newlist) +" --->");
					log.info("Sending Rcvd Installment To SAP Size = "+ CommonService.getSize(newlist) +" --->");
				}
				
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	@Deprecated
	private void Start() throws Exception {
		
		try{
			
			String company = "SP";
			String project = "CHS";  //SPLCITY-SRI //SPLVILLE-PRAGSA
			String ReceipteddocumentNo = "RR-1207-000010";
			String status = "A";
			String syncIsstaging = "N";
			
			Processing(company, project, ReceipteddocumentNo, status, syncIsstaging);			
			
			/*
			//Find Receipt
			List<VwSyncRcvdInstallment> lstt = new VwSyncRcvdInstallmentAction().findRcvdInstallmentByDocumentNo(company, project, ReceipteddocumentNo, status, syncIsstaging);
			
			//Property Unit///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			PropertyUnitAction propertyUnitAction = new PropertyUnitAction();
			List<PsWbsMaster> unitLst = propertyUnitAction.CreatedToStaging(company, project, lstt.get(0).getBuildingPhaseCode(), lstt.get(0).getFloorZoneCode(), lstt.get(0).getUnitNumber());
			log.info("Create Property Unit to from BS,CS Staging ============================================>");
			
			new PsWbsMasterService().SendingToSAP(unitLst);
			log.info("Sending Property Unit "+ unitLst.get(0).getUnitNumber() +" from BS,CS to SAP  ============================================>");
			
			//Create to CreateRcvdToStaging
			//List<FiArreceiptreOutb> RR_Installment_lst = new VwSyncRcvdInstallmentAction().CreateRcvdToStaging(company, project, ReceipteddocumentNo, status, syncIsstaging);
			List<FiArreceiptreOutb> RR_Installment_lst = new VwSyncRcvdInstallmentAction().CreateRcvdToStaging(lstt);
			log.info("Created Rcvd Installment To Staging ==========================================>");
			
			//Create ChargeCode To Staging 
			ReceiptedChargeCodeAction receiptedChargeCodeAction = new ReceiptedChargeCodeAction();			
		    List<SdChargecode> newValueList = receiptedChargeCodeAction.CreateToStaging(RR_Installment_lst);
			
			//Sending ChargeCode to SAP (SD)
			SdChargecodeService SdChargecodeServ = new SdChargecodeService();
			SdChargecodeServ.SendingToSAP(newValueList);
			log.info("Sending Rcvd ChargeCode To Staging ==========================================>");
			
			//Send to RCVD SAP (FI)
			new FiArreceiptreOutbService().SendingToSAP(RR_Installment_lst);
			log.info("Sending Rcvd Installment To SAP ==========================================>");
			*/
			
			/*
			//Create to CreateRcvdToStaging
			log.info("Start Create Rcvd Installment To Staging ==========================================>");
			List<FiArreceiptreOutb> RR_Installment_lst = new VwSyncRcvdInstallmentAction().CreateRcvdToStaging(company, project, ReceipteddocumentNo, status, syncIsstaging);
			log.info("End Create Rcvd Installment To Staging ==========================================>");
			 
			//Create ChargeCode To Staging 
			ReceiptedChargeCodeAction receiptedChargeCodeAction = new ReceiptedChargeCodeAction();			
		    List<SdChargecode> newValueList = receiptedChargeCodeAction.CreateToStaging(RR_Installment_lst);
			
			//Sending ChargeCode to SAP (SD)
			log.info("Start Sending Rcvd ChargeCode To Staging ==========================================>");
			SdChargecodeService SdChargecodeServ = new SdChargecodeService();
			SdChargecodeServ.SendingToSAP(newValueList);
			
			//Send to RCVD SAP (FI)
			log.info("Start Sending Rcvd Installment To SAP ==========================================>");
			new FiArreceiptreOutbService().SendingToSAP(RR_Installment_lst);
			log.info("End Sending Rcvd Installment To SAP ==========================================>");
			*/
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try{
			
			new ReceiptedInstallmentProcessing().SendAllWithNonSending();
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}

	}

}
